#include "music.h"

#include "music.h"
#include <QUuid>
#include <QMediaPlayer>
#include <QCoreApplication>
#include <QSqlError>

Music::Music(QUrl url)
    :isLike(false),
     isHistory(false),
     musicUrl(url)
{
    //读取url对应的歌曲文件的信息,解析出原数据
    //歌曲名称,歌曲作者,歌曲专辑,歌曲持续时间
    musicId = QUuid::createUuid().toString();

    praseMusic();

}

Music::Music()
{

}

void Music::setMusicName(const QString& musicname)
{
    this->musicName = musicname;
}
void Music::setMusicSinger(const QString& musicsinger)
{
    this->musicSinger = musicsinger;
}
void Music::setMusicAlbum(const QString& musicAlbum)
{
    this->musicAlbumn = musicAlbum;
}
void Music::setMusicDuration(qint64 duration)
{
    this->duration = duration;
}
void Music::setIsLike(bool isLike)
{
    this->isLike = isLike;
}
void Music::setIsHistory(bool isHistory)
{
    this->isHistory = isHistory;
}
void Music::setMusicUrl(QUrl musicurl)
{
    this->musicUrl = musicurl;
}

void Music::setMusicId(const QString &musicId)
{
    this->musicId = musicId;
}

QString Music::getMusicName() const
{
    return this->musicName;
}
QString Music::getMusicSinger() const
{
    return this->musicSinger;
}
QString Music::getMusicAlbum() const
{
    return this->musicAlbumn;
}
qint64 Music::getMusicDuration() const
{
    return this->duration;
}
bool Music::getIsLike() const
{
    return this->isLike;
}
bool Music::getIsHistory() const
{
    return this->isHistory;
}
QUrl Music::getMusicUrl() const
{
    return this->musicUrl;
}

QString Music::getMusicId() const
{
    return this->musicId;
}

QString Music::getMusicFilePath() const
{
    QString musicPath = musicUrl.toLocalFile();
    musicPath.replace(".mp3",".lrc");
    musicPath.replace(".flac",".lrc");
    musicPath.replace(".mpga",".lrc");

    return musicPath;
}

void Music::InsertMusicToDB()
{
    // 1. 检测music是否在数据库中存在
    QSqlQuery query;
    query.prepare("select exists(select 1 from MusicInfo where musicId = ?)");

    query.addBindValue(musicId);

    if (!query.exec())
    {
        qDebug() << "查询失败" << query.lastError().text();
        return;
    }

    if (!query.next())
    {
        return;
    }

    bool isExists = query.value(0).toBool();

    if (isExists)
    {
        // 存在, 不需要直接插入Music对象,此时只需要将isLike和isHistory属性进行更新

        query.prepare("update MusicInfo set isLike = ?,isHistory = ? where musicId = ?");
        query.addBindValue(isLike ? 1 : 0);
        query.addBindValue(isHistory ? 1 : 0);
        query.addBindValue(musicId);

        if (!query.exec())
        {
            qDebug() << "更新失败: " << query.lastError().text();
        }

        qDebug() << "更新music成功: " << musicName << " " << musicId;
    }
    else
    {
        // 不存在, 直接将Music的属性信息插入数据库
        query.prepare("insert into MusicInfo(musicId,musicName,musicSinger,musicAlbumn,musicUrl,duration,isLike,isHistory) values (?,?,?,?,?,?,?,?)");
        query.addBindValue(musicId);
        query.addBindValue(musicName);
        query.addBindValue(musicSinger);
        query.addBindValue(musicAlbumn);
        query.addBindValue(musicUrl.toString());//?
        query.addBindValue(duration);
        query.addBindValue(isLike ? 1 : 0);
        query.addBindValue(isHistory ? 1 : 0);

       if (!query.exec())
       {
           qDebug() << "插入信息失败: " << query.lastError().text();
           return;
       }

       qDebug() << "插入信息成功: " << musicName << " " << musicId;
    }


    // 2. 存在:不需要在插入music对象,此时,只需要将isLike和isHistory属性进行更新即可


    // 3. 不存在:直接将music的属性信息插入数据库中
}

void Music::praseMusic()
{
    //1. 创建媒体播放对象
    QMediaPlayer player;

    //2. 依靠setMedia方法解析元数据
    player.setMedia(musicUrl);

    while (!player.isMetaDataAvailable()) {
       QCoreApplication::processEvents();
    }

    if (player.isMetaDataAvailable())
    {
        musicName = player.metaData("Title").toString();
        musicSinger = player.metaData("Author").toString();

        musicAlbumn = player.metaData("AlbumTitle").toString();
        duration = player.duration();

        QString fileName = musicUrl.fileName();
        //找-
        int index = fileName.indexOf('-');
        if (musicName.isEmpty())
        {

            if (index != -1)
            {
                //2002年的第一场雪 - 刀郎.mp3
                musicName = fileName.mid(0,index).trimmed();
            }
            else
            {
                //2002年的第一场雪.mp3
                musicName = fileName.mid(0,fileName.indexOf('.')).trimmed();
            }

        }

        //作者为空
        if (musicSinger.isEmpty())
        {
            if (index != -1)
            {
                musicSinger = fileName.mid(index + 1,fileName.indexOf('.') - index - 1).trimmed();
            }
            else
            {
                musicSinger = "未知歌手";
            }
        }

        if (musicAlbumn.isEmpty())
        {
            musicAlbumn = "未知专辑";
        }

        qDebug() << musicName << ":" << musicSinger << ":" << musicAlbumn << ":" << duration;

    }
}
